package com.gerryrun.lib_skin;

import android.content.res.ColorStateList;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.view.ViewCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GerryRun
 * @data 2021/1/27
 * @email gerryin@163.com
 * @describe 换肤的对象 ，所需的属性
 */
public class SkinAttribute {

    private static final List<String> mAttributes = new ArrayList<>();

    static {
        mAttributes.add("background");
        mAttributes.add("src");
        mAttributes.add("textColor");
//        mAttributes.add("drawableLeft");
//        mAttributes.add("drawableRight");
//        mAttributes.add("drawableTop");
//        mAttributes.add("drawableBottom");
    }

    private List<SkinView> skinViews = new ArrayList<>();

    /**
     * 对所有UI进行更新
     */
    public void applySkin() {
        for (SkinView skinView : skinViews) {
            skinView.applySkin();
        }
    }

    public void look(View view, AttributeSet attrs) {
        List<SkinPair> skinPairs = new ArrayList<>();
        for (int i = 0; i < attrs.getAttributeCount(); i++) {

            String attrName = attrs.getAttributeName(i);
            if (mAttributes.contains(attrName)) {
                String attrValue = attrs.getAttributeValue(i);
                //写死的色值不进行替换 换肤
                if (attrValue.startsWith("#")) {
                    continue;
                }
                int resId = 0;
                if (attrValue.startsWith("@")) {
                    resId = Integer.parseInt(attrValue.substring(1));
                }

                SkinPair pair = new SkinPair(attrName, resId);
                skinPairs.add(pair);
            }
        }

        if (!skinPairs.isEmpty() || view instanceof SkinViewSupport) {
            SkinAttribute.SkinView skinView = new SkinAttribute.SkinView(view, skinPairs);
            // 如果选择过皮肤 ，调用 一次 applySkin 加载皮肤的资源
            skinView.applySkin();
            skinViews.add(skinView);
        }
    }

    static class SkinView {

        private static final String TAG = "SkinView";
        View view;

        List<SkinPair> skinPairs;
        private Object background;

        public SkinView(View view, List<SkinPair> skinPairs) {
            this.view = view;
            this.skinPairs = skinPairs;
        }

        public void applySkin() {
            for (SkinPair pair : skinPairs) {
                Drawable left = null, top = null, right = null, bottom = null;
                switch (pair.attributeName) {
                    case "background":
                        Object background = SkinResource.getInstance().getBackground(pair
                                .resId);
                        if (background instanceof Integer) {
                            view.setBackgroundColor((Integer) background);
                        } else {
                            ViewCompat.setBackground(view, (Drawable) background);
                        }
                        Log.e(TAG, "applySkin: background==" + background);
                        break;
                    case "textColor":
                        ColorStateList colorStateList = SkinResource.getInstance().getColorStateList
                                (pair.resId);
                        ((TextView) view).setTextColor(colorStateList);
                        Log.e(TAG, "applySkin: colorStateList==" + colorStateList);
                        break;
                    case "src":
                        background = SkinResource.getInstance().getBackground(pair.resId);
                        if (background instanceof Integer) {
                            ((ImageView) view).setImageDrawable(new ColorDrawable((Integer) background));
                        } else {
                            ((ImageView) view).setImageDrawable((Drawable) background);
                        }
                        break;
                }
            }
        }
    }

    static class SkinPair {
        //属性名
        String attributeName;
        int resId;

        public SkinPair(String attributeName, int resId) {
            this.attributeName = attributeName;
            this.resId = resId;
        }
    }
}
